package logicaDePredicados;

import java.util.HashMap;
import java.util.Iterator;

import exceptions.UnDecidibleValueException;

public class LogicPredicate <T> extends PredicateLogic{
	private HashMap <T,Boolean> valueMap;
	/**
	 * predicate function constructor
	 * @param name
	 */
	public LogicPredicate(String name) {
		super();
		this.setName(name);
		valueMap = new HashMap<T, Boolean> ();
		
	}
	/**
	 * Returns the value corresponding to the domain of the function. Throws exception when
	 * an invalid value is used.
	 * @param object
	 * @return
	 * @throws UnDecidibleValueException
	 */
	public Boolean getTruthValue (Object object) throws UnDecidibleValueException{
		Boolean returnValue = valueMap.get(object);
		if (returnValue==null)
			throw new UnDecidibleValueException(object.toString()+" not found in predicate "+getName());
		return returnValue;
	}
	/**
	 * Adds a valid constant value and its corresponding boolean value to 
	 * the domain of this predicate function
	 * @param value
	 * @param truthValue
	 */
	public void addTruthValue (T value, boolean truthValue){
		valueMap.put(value,new Boolean(truthValue));
	}
	public String toString (){
		StringBuffer returnValue=new StringBuffer();
		Iterator<T> i = valueMap.keySet().iterator();
		while (i.hasNext()){
			T keyValue = i.next();
			Boolean value=valueMap.get(keyValue);
			returnValue.append(this.getName()+"("+keyValue+")"+"\t"+value+"\n");
			
		}
			
		
		return returnValue.toString();
	}
	
	@Override
	public boolean equals (Object o){
		/**
		 * if the objects are indeed the same class then compare their names
		 */
		if (o.getClass()==this.getClass()){
			return this.getName().equalsIgnoreCase(((LogicPredicate<?>)o).getName());
		}
		return false;
		
	}
}
